package saucaca.Util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

public class FileUtils {

	public static void close(Reader reader) {
		if (reader != null)
			try {
				reader.close();
			} catch (IOException e) {
			}
	}

	public static void close(Writer writer) {
		if (writer != null)
			try {
				writer.close();
			} catch (IOException e) {
			}
	}

	public static void close(InputStream inputStream) {
		if (inputStream != null)
			try {
				inputStream.close();
			} catch (IOException e) {
			}
	}

	public static void close(OutputStream outputStream) {
		if (outputStream != null)
			try {
				outputStream.close();
			} catch (IOException e) {
			}
	}

	public static String getFileNameString(String str) {
		char[] array = str.toCharArray();
		int pos1 = 0;// the last file separator position
		int pos2 = 0;// the last but one file separator position
		for (int i = array.length - 1; i >= 0; i--) {
			if (array[i] == File.separatorChar) {
				if (pos1 == 0)
					pos1 = i;
				else {
					pos2 = i;
					break;
				}
			}
		}
		return str.substring(pos2 + 1, pos1);
	}

	// get song file url substring
	public static String getFileUrlString(String str) {
		char[] array = str.toCharArray();
		int pos = 0;// the last file separator position
		for (int i = array.length - 1; i >= 0; i--) {
			if (array[i] == File.separatorChar) {
				pos = i;
				break;
			}
		}
		return str.substring(0, pos + 1);
	}
}
